package edu.gatech.cs2340.mule.model;
import java.util.Random;

/**
 * This class holds the logic for deciding whether a random event will occur
 * for a player and if so, which one actually happens.
 * 
 * @author Ryan Swing
 *
 */
public class RandomEvent {
	
	private static final int[] M_BONUS = {0, 25, 25, 25, 50, 50, 50, 50, 75, 75, 75, 75, 100};
	
	private String message;
	private int event;
	
	/**
	 * Creates a new random event for the player. Decides logic based on
	 * the current round of the game.
	 * @param player
	 * @param currentRound
	 */
	public RandomEvent(Player player, int currentRound) {
		message = "";
		Random rand = new Random();
		event = 0;
		
		if(player.isInLast()) {
			event = rand.nextInt(4); //pick only good events for last place
		}
		else {
			event = rand.nextInt(7);
		}
			
		if(rand.nextInt(100) < 27) {
			updatePlayer(player, event, currentRound);
		}
	}
	
	/**
	 * Chooses a random event from the list and then applies its effects to
	 * the designated player
	 * 
	 * @param player the player that will receive the effects of the random event
	 * @param event the index of the event in the event array
	 * @param currentRound
	 */
	public void updatePlayer(Player player, int event, int currentRound){
		int m = calculateMBonus(currentRound);
		
		if (event == 0) {
			player.setFoodAmount(3);
			player.setEnergyAmount(2);
			message = player.getPlayerName() + " just received a package from " +
					"the GT Alumni containing 3 food and 2 energy units.";
		}
		else if(event == 1) {
			player.setSmithOreAmount(2);
			message = "A wandering tech student repaid " + player.getPlayerName() +
					"'s hospitality by leaving two bars of ore.";
		}
		else if(event == 2) {
			player.setMoneyAmount(8 * m);
			message = "The museum bought " + player.getPlayerName() + "'s antique" +
					" personal computer for $" + 8 * m + ".";
		}
		else if(event == 3) {
			player.setMoneyAmount(2 * m);
			message = player.getPlayerName() + " found a dead moose rat and sold " +
					"the hide for $" + 2 * m + ".";
		}
		else if(event == 4) {
			player.setMoneyAmount(-4 * m); 
			message = "Flying cat-bugs ate the roof off " + player.getPlayerName() +
					"'s house. Repairs cost $" + 4 * m + ".";
		}
		else if(event == 5) {
			player.setFoodAmount(-player.getFoodAmount() / 2);
			message = "Mischievous UGA students broke into " +
			player.getPlayerName() + "'s storage shed and stole half of their food.";
		}
		else {
			player.setMoneyAmount(-6 * m);
			message = player.getPlayerName() + "'s space gypsy inlaws made a mess" +
					" of the town. It costs them $" + 6 * m + " to clean it up.";
		}
	}
	
	/**
	 * 
	 * @return the String representation of what effect the event had
	 */
	public String getMessage() {
		return message;
	}
	
	/**
	 * 
	 * Calculates the m bonus using the current round of the game
	 * 
	 * @param currentRound The current round of the game
	 * @return The m bonus used to calculate the overall cost of a random event
	 */
	private int calculateMBonus(int currentRound) {
		return M_BONUS[currentRound];
	}
	
	/**
	 * 
	 * @return whether or not an event occurred based on whether or not
	 * the message variable is empty
	 */
	public boolean eventOccurred() {
		return !("".equals(message));
	}
	
	/**
	 * 
	 * @return the corresponding index for the random event
	 */
	public int getEventIndex() {
		return event;
	}
}